package com.edou.cms.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.cms.dao.ICmsBlogAuthorDao;
import com.edou.cms.dao.ICmsBlogAuthorTagDao;
import com.edou.cms.dao.ICmsTagDao;
import com.edou.cms.entity.CmsBlogAuthor;
import com.edou.cms.entity.CmsBlogAuthorTag;
import com.edou.cms.service.ICmsBlogAuthorService;

import com.edou.cms.dto.CmsBlogAuthorCondition;

@Service("cmsBlogAuthorServiceImpl")
@Transactional(value = "transactionManager")
public class CmsBlogAuthorServiceImpl implements ICmsBlogAuthorService{

	@Resource(name = "cmsBlogAuthorDaoImpl")
	private ICmsBlogAuthorDao cmsBlogAuthorDao;
	
	@Resource(name = "cmsBlogAuthorTagDaoImpl")
	private ICmsBlogAuthorTagDao cmsBlogAuthorTagDao;
	
	@Resource
	private ICmsTagDao cmsTagDao;

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listCmsBlogAuthorByPage(PageResult<CmsBlogAuthor> pageResult,CmsBlogAuthorCondition condition){
		int rows = cmsBlogAuthorDao.listCmsBlogAuthorByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<CmsBlogAuthor> list = cmsBlogAuthorDao.listCmsBlogAuthorByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 博客博主
	 */
	public CmsBlogAuthor getCmsBlogAuthorById(String id){
		return cmsBlogAuthorDao.getCmsBlogAuthorById(id);
	}

	/**
	 * 新增
	 * @param item 博客博主
	 */
	public void add(CmsBlogAuthor item){
		// 默认值设为0
		item.setNumArticle(0);
		item.setCountCollect(0);
		item.setCountView(0);
		item.setCountLike(0);
		cmsBlogAuthorDao.add(item);
		String tagList = item.getTagList();
		if(StringUtils.isNotBlank(tagList)){
			String[] tagIds = tagList.split(",");
			// 博主与标签关联表 cms_blog_author_tag
			for (int i = 0; i < tagIds.length; i++) {
				CmsBlogAuthorTag tag = new CmsBlogAuthorTag();
				tag.setBlogAuthorId(item.getId());
				tag.setTagId(tagIds[i]);
				tag.setDeleteFlag(0);
				cmsBlogAuthorTagDao.add(tag);
				//修改标签下的文章数量
				cmsTagDao.updateCmsTagArticle(tagIds[i],"zj");
			}
		}
	}

	/**
	 * 修改
	 * @param item 博客博主
	 */
	public void update(CmsBlogAuthor item){
		cmsBlogAuthorDao.update(item);
		//关联标签数量重置、
		List<CmsBlogAuthorTag> cmsBlogAuthorTagList = cmsBlogAuthorTagDao.listCmsBlogAuthorTagByAuthorId(item.getId());
		for (CmsBlogAuthorTag cmsBlogAuthorTag : cmsBlogAuthorTagList) {
			cmsTagDao.updateCmsTagArticle(cmsBlogAuthorTag.getTagId(),"jx");
		}
		//删除已有标签
		cmsBlogAuthorTagDao.delByAuthorId(item.getId());
		//添加新标签
		String tagList = item.getTagList();
		if(StringUtils.isNotBlank(tagList)){
			String[] tagIds = tagList.split(",");
			for (int i = 0; i < tagIds.length; i++) {
				CmsBlogAuthorTag tag = new CmsBlogAuthorTag();
				tag.setBlogAuthorId(item.getId());
				tag.setTagId(tagIds[i]);
				tag.setDeleteFlag(0);
				cmsBlogAuthorTagDao.add(tag);
				//修改标签下的文章数量
				cmsTagDao.updateCmsTagArticle(tagIds[i],"zj");
			}
		}
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		cmsBlogAuthorDao.delByIds(ids);
	}

	/**
	 * 查询所有博客
	 */
	public List<CmsBlogAuthor> listCmsBlogAuthorAll(CmsBlogAuthorCondition condition) {
		return cmsBlogAuthorDao.listCmsBlogAuthorAll(condition);
	}

	@Override
	public void delByAuthorId(String authorId) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 修改热度
	 */
	public void updateCountView(String id) {
		cmsBlogAuthorDao.updateCountView(id);
	}

	/**
	 *  修改收藏数
	 */
	public void updateCountCollect(String id) {
		cmsBlogAuthorDao.updateCountCollect(id);
	}

	/**
	 * 查询最受欢迎自媒体
	 */
	public List<CmsBlogAuthor> listCmsBlogAuthorWell(Integer limit) {
		return cmsBlogAuthorDao.listCmsBlogAuthorWell(limit);
	}

	/**
	 * 查询推荐的自媒体
	 */
	public List<CmsBlogAuthor> listCmsBlogAuthoRec(Integer limit) {
		return cmsBlogAuthorDao.listCmsBlogAuthoRec(limit);
	}

	/**
	 * 查询推荐的博主
	 */
	public List<CmsBlogAuthor> listCmsBlogAuthorTop(Integer limit,
			CmsBlogAuthorCondition condition) {
		return cmsBlogAuthorDao.listCmsBlogAuthorTop(limit, condition);
	}


}

